const app = getApp();
const publicFun = require('/utils/public.js');
import { userFeedBack } from '/utils/request'
Page({
  data: {
    textareaVal: '',
    contactVal: '',
    disabled: false,
    pictureList: [],
    uploadImgList:[], //图片路径列表
  },
  onLoad() {
    this.setData({
      pictureList: [],
      uploadImgList:[],
      systemInfo: app.globalData.systemInfo
    })
    my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff',
    })
  },
  onTextareaInput(e) {
    this.setData({
      textareaVal: e.detail.value
    })
  },
  onContactTxtarea(e) {
    this.setData({
      contactVal: e.detail.value
    })
  },
  onAddImg() {
    var that = this;
    my.chooseImage({
      sourceType: ['album'],
      count: 3,
      success: (res) => {
        const path = res.apFilePaths;
        res.apFilePaths.forEach((imgItem) => {
          that.data.pictureList.push(imgItem);
        });
        this.setData({
          pictureList: this.data.pictureList
        });
      },
    })
  },
  onDeleteImg(e) {
    this.data.pictureList.splice(e.target.dataset.index, 1);
    this.setData({
      pictureList: this.data.pictureList
    });
  },
  submitBtn() {
    if(this.data.textareaVal) {
      my.showLoading();
      let uploads =[];
      this.data.pictureList.forEach((item, index)=>{  // item为图片的url
        uploads.push(publicFun.commonUpLoadImg(publicFun.api.commonImg, item, {}));
      })
      Promise.all(uploads).then(res => {
        res.forEach((item) => {
          if(item.S == 1){
            this.data.uploadImgList.push(item.rpath);
            this.setData({
              uploadImgList: this.data.uploadImgList
            });
          }else{
            publicFun.showToast(res.M ? res.M : '请求失败');
          }
        });

        this.bindFormSubmit(this.data.uploadImgList);
      }).catch(() => {
        my.hideLoading();
        publicFun.showToast("修改失败")
      });
    }else{
      publicFun.showToast('请填写建议');
    }
  },
  async bindFormSubmit(uploadImgList) {
    let picsStr = '';
    if(this.data.pictureList.length > 0){
      for(let i=0;i<uploadImgList.length;i++){
        picsStr += uploadImgList[i]+'|'
      }
    }
    try {
      let VersionInfo = this.data.systemInfo.brand+'-'+this.data.systemInfo.model+'-'+this.data.systemInfo.system;
      var params = {
        UserId: app.globalData.userid,
        Message: this.data.textareaVal,
        IOSOrAndroid: '支付宝气候行动小程序', // ios或者安卓
        System: this.data.systemInfo.platform, // 系统-系统名字
        Version: VersionInfo, // 版本
        AppVersion: this.data.systemInfo.version, //APP版本-支付宝版本
        Location: app.globalData.location.lng+','+app.globalData.location.lat,  //定位
        Pics: picsStr.slice(0,picsStr.length-1), //图片
        Contactmethord: this.data.contactVal, 
        ParentId: 0,
      }
      const res = await userFeedBack(params)
      if (res.S == 1) {
        my.hideLoading();
        my.showToast({
          type: 'none',
          content: res.M,
          duration: 1000,
          success: () => {
            my.navigateBack();
          },
        });
        this.setData({
          textareaVal: '',
          contactVal: '',
          pictureList: [],
          uploadImgList:[],
        });
      }else{
        publicFun.showToast(res.M ? res.M : '操作失败');
      }
    } catch (e) {
      publicFun.showToast('失败，请稍后再试')
    }
  },
  jumpFeedbackList() {
    my.navigateTo({
      url: '../myFeedbackList/myFeedbackList'
    });
  }
});
